package cn.lyjuan.android.demofive.frame.kotlin

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import cn.lyjuan.android.demofive.R
import cn.lyjuan.android.demofive.databinding.FrameKotlinActivityBinding
import cn.lyjuan.android.demofive.databinding.FrameKotlinItemBinding
import cn.lyjuan.android.demofive.databinding.FrameRealmRecyclerAddBinding
import cn.lyjuan.android.demofive.frame.realm.UserModel
import cn.lyjuan.android.mylibrary.context.RealmApplication
import cn.lyjuan.base.util.StringUtils
import io.realm.Realm
import io.realm.RealmResults

/**
 * Created by chad on 08/11/2017.
 */
class KotlinRealmDatabindingActivity : AppCompatActivity() {

    private var binding: FrameKotlinActivityBinding? = null

    private var realm: Realm? = null

    private var list: RealmResults<UserModel>? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.frame_kotlin_activity)

        realm = RealmApplication.realm()

        list = UserModel.findAll(realm)

        setView()
    }

    private fun setView() {

        binding!!.recyclerView.adapter = object : RecyclerView.Adapter<ViewBindingHolder>() {
            override fun onBindViewHolder(holder: ViewBindingHolder?, position: Int) {
                var user = list!![position]
                holder!!.binding!!.user = user
                holder!!.binding.clickListener = View.OnClickListener {
                    Snackbar.make(holder.binding.root, "click >> " + user!!.name, Snackbar.LENGTH_SHORT).show()
                }
                holder!!.binding.onLongClickListener = View.OnLongClickListener {
                    holder.binding.user!!.del()
                    binding!!.recyclerView.adapter.notifyItemRemoved(holder!!.layoutPosition)
                    true
                }

            }

            override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewBindingHolder {
                var binding = DataBindingUtil.inflate<FrameKotlinItemBinding>(layoutInflater, R.layout.frame_kotlin_item, parent, false) as FrameKotlinItemBinding

                return ViewBindingHolder(binding)
            }

            override fun getItemCount(): Int {
                return list?.size ?: 0
            }
        }
    }

    private var dialog: AlertDialog? = null

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {

        if (item?.itemId != 1)
            return false

        if (null == dialog) {
            val builder = AlertDialog.Builder(this)

            val addBinding = DataBindingUtil.inflate<FrameRealmRecyclerAddBinding>(layoutInflater, R.layout.frame_realm_recycler_add, null, false)

            builder.setTitle("添加").setView(addBinding.root)
                    .setPositiveButton("确定") { dialog, which ->
                        val name = addBinding.name.text.toString()
                        val age = addBinding.age.text.toString()
                        // 添加
                        if (StringUtils.isNull(name) || StringUtils.isNull(age)) {
                            Snackbar.make(binding!!.recyclerView, "属性不能为空", Snackbar.LENGTH_SHORT).show()
                            return@setPositiveButton
                        }

                        // 添加用户
                        UserModel.add(realm, name, Integer.parseInt(age))
                    }

            dialog = builder.create()
        }

        dialog!!.show()
        return true
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {

        menu?.add(1, 1, 1, "添加")?.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)

        return true
    }


    class ViewBindingHolder(val binding: FrameKotlinItemBinding) : RecyclerView.ViewHolder(binding.root)
}